package com.bases.ScollBar
{
	import com.Timer.FrameTimerEvent;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	
	public class TTextFiledScrollBar extends TBaseScrollBar
	{
		protected var scrollText:TextField;
		
		public function TTextFiledScrollBar(txt:TextField)
		{
			this.scrollText=txt;
			this.scrollText.addEventListener(Event.SCROLL,onScrollEvent);
			super();
		}
		
		override protected function onBackBGDown(event:MouseEvent):void{
			super.onBackBGDown(event);
			scrollText.scrollV = (scrollText.maxScrollV - 1) * (btnScroll.y - poleStartY) / totalPixels + 2;
			var unitPixels : Number = totalPixels / (scrollText.maxScrollV - 1);
			if ((btnScroll.y - poleStartY) < unitPixels) 
			{
				scrollText.scrollV = (scrollText.maxScrollV - 1) * (btnScroll.y - poleStartY) / totalPixels + 1;
			}
		}
		
		private function onScrollEvent(event:Event):void{
			if(this.scrollText.maxScrollV!=1){
				this.visible=true;
				//定义一个高度因子，此因子随加载文本的增多，将无限趋向于1
				var heightVar : Number = 1 - (scrollText.maxScrollV - 1) / scrollText.maxScrollV;
				this.setThumbHeight(heightVar,this.scrollText.scrollV,this.scrollText.maxScrollV);
			}else{
				this.visible=false;
			}
		}
		
		override public function setSize(w:Number, h:Number):void{
			super.setSize(w,h);
			this.onScrollEvent(null);
		}
		
		override protected function onUp(event:MouseEvent):void{
			scrollText.scrollV--;
			super.onUp(event);
		}
		
		override protected function addTargetEvent(_target:Sprite,type:String):void{
			this.scrollText.removeEventListener(Event.SCROLL,onScrollEvent);
			super.addTargetEvent(_target,type);
		}
		
		override protected function onTargetUp(event:MouseEvent):void{
			if(isBtnDown){
				this.scrollText.addEventListener(Event.SCROLL,onScrollEvent);
				super.onTargetUp(event);
			}
		}
		
		override protected function onDown(event:MouseEvent):void{
			scrollText.scrollV++;
			super.onDown(event);
		}
		
		override protected function onTimer(event:FrameTimerEvent):void{
			if(type=='down'){
				scrollText.scrollV++;
			}else{
				scrollText.scrollV--;
			}
			super.onTimer(event);
		}
		
		override protected function thumbScrollV():void{
			this.btnScroll.y = Math.floor(poleStartY + totalPixels * (scrollText.scrollV - 1) / (scrollText.maxScrollV - 1));
		}
		
		override protected function onMouseDown(event:MouseEvent):void{
			scrollText.removeEventListener(Event.SCROLL, onScrollEvent);
			super.onMouseDown(event);
		}
		
		override protected function onStateMove(event:MouseEvent):void{
			//在滚动过程中及时获得滑块所处位置
			var nowPosition : Number = Math.floor(this.btnScroll.y);
			//使文本随滚动条滚动,这里为什么要加1，可见scroll属性值应该是取正的，也就是说它会删除小数部分，而非采用四舍五入制？
			scrollText.scrollV = (scrollText.maxScrollV - 1) * (nowPosition - poleStartY) / totalPixels + 2;
			//误差校正
			var unitPixels : Number = totalPixels / (scrollText.maxScrollV - 1);
			if ((nowPosition - poleStartY) < unitPixels) 
			{
				scrollText.scrollV = (scrollText.maxScrollV - 1) * (nowPosition - poleStartY) / totalPixels;
			}
		}
		//鼠标弹起,或鼠标离开
		override protected function onMouseUp(event:MouseEvent):void{
			this.scrollText.addEventListener(Event.SCROLL,onScrollEvent);
			super.onMouseUp(event);
		}
	}
}